<?php
/**
 * Author: Administrator
 * Date: 2022/7/23 15:15
 */

namespace DesignPatterns\Behavioral\ChainOfResponsibilities;


abstract class Handler
{
    /**
     * @var Handler
     */
    protected ?Handler $successor = null;

    final public function addChain(Handler $handler) {
        if (!is_null($this->successor)) {
            $this->successor->addChain($handler);
        } else {
            $this->successor = $handler;
        }
    }

    final public function handle(Request $request) {
        if ($this->process($request)) {
            $request->processor = get_called_class();
            return true;
        }
        if ($this->successor) {
            return $this->successor->handle($request);
        }
        return false;
    }

    abstract protected function process(Request $request);

}